//
//  Signature.m
//  Opper
//
//  Created by Le Dung on 4/19/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "Signature.h"

@interface Signature ()

@end

@implementation Signature

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    [StyleClass barButtonItemEdit:self left:YES right:NO];
}
-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}




-(void)acceptPress
{
    
//    UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"Use this signature" message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"don't use !", nil];
//    alertView.delegate =self;
//    [alertView show ];
//    [alertView release];
    
    [self save];
    [self.navigationController  popViewControllerAnimated:YES];


}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self save];
            
            
            break;
            
        default:
            break;
    }
}
-(void)cancelPress
{
    [self reset];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [StyleClass createNavi:self andTitle:@"Ondertekenen"];
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self buttonTarget];
    red = 0.0/255.0;
    green= 0.0/255.0;
    blue= 0.0/255.0;
    brush=1;
    opacity=1.0;
    self.signature =[[Signature alloc]init];
    
}
-(void)buttonTarget
{
    [self.acceptButton addTarget:self action:@selector(acceptPress) forControlEvents:UIControlEventTouchUpInside];
    [self.acceptButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setBackgroundImage:[StyleClass nRed] forState:UIControlStateNormal];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    mouseSwipe=NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    mouseSwipe=YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    
    
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempDrawImage.image=UIGraphicsGetImageFromCurrentImageContext();
    [self.tempDrawImage setAlpha:opacity];
    UIGraphicsEndImageContext();
    
    lastPoint=currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (!mouseSwipe) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.tempDrawImage.image=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    UIGraphicsBeginImageContext(self.mainImage.frame.size);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)blendMode:kCGBlendModeNormal alpha:1.0];
    self.mainImage.image=UIGraphicsGetImageFromCurrentImageContext();
    self.tempDrawImage.image=nil;
    UIGraphicsEndImageContext();
}

-(void)cancelPencil:(UIButton*)sender
{
    [sender setFrame:CGRectMake(sender.frame.origin.x, sender.frame.origin.y+10, sender.frame.size.width, sender.frame.size.height)];
    
}


-(IBAction)reset
{
    self.mainImage.image=nil;
}
-(IBAction)save
{
    
    UIGraphicsBeginImageContextWithOptions(self.mainImage.bounds.size, NO, 0.0);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)];
    UIImage *saveImg = UIGraphicsGetImageFromCurrentImageContext();
    [GetData App].signImage=saveImg;

    UIGraphicsEndImageContext();
//    UIImageWriteToSavedPhotosAlbum(saveImg, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
	UIAlertView *alert;
	
	// Unable to save the image
	if (error)
		alert = [[UIAlertView alloc] initWithTitle:@"Save Image"
										   message:@"UnSuccessful"
										  delegate:nil cancelButtonTitle:@"OK"
								 otherButtonTitles:nil];
	else
    {
		alert = [[UIAlertView alloc] initWithTitle:nil
										   message:@"Successful"
										  delegate:nil cancelButtonTitle:@"OK"
								 otherButtonTitles:nil];
    
    }
	[alert show];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [_acceptButton release];
    [_cancelButton release];
    [super dealloc];
}
@end
